package cn.xopencode.appletweb.controller.record.pdroom;

import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.recordservice.rpc.pdroom.PowerDistributionRoomRpc;
import cn.xopencode.recordservice.rpc.pdroom.dto.PowerDistributionRoomCreateDTO;
import cn.xopencode.recordservice.rpc.pdroom.vo.PowerDistributionRoomVO;
import cn.xopencode.systemservice.rpc.datadict.DataDictRpc;
import cn.xopencode.systemservice.rpc.datadict.vo.DataDictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "配电室管理 API")
@RestController
@RequestMapping("/pdroom")
@Validated
public class PowerDistributionRoomController {

    @DubboReference(version = "${dubbo.consumer.PowerDistributionRoomRpc.version}")
    private PowerDistributionRoomRpc powerDistributionRoomRpc;

    @DubboReference(version = "${dubbo.consumer.DataDictRpc.version}")
    private DataDictRpc dataDictRpc;

    private String pdroomDict = "pdroom_area";

    /**
     * 根据配电室名称模糊匹配所有的配电室
     * @param roomName
     * @return
     */
    @GetMapping("/get")
    @ApiOperation("根据配电室名称模糊匹配所有的配电室")
    @ApiImplicitParam(name = "roomName", value = "配电室名称", required = false)
    public CommonResult<List<PowerDistributionRoomVO>> getPowerDistributionRoomVO(
            @RequestParam(name = "roomName", required = false) String roomName){
        CommonResult<List<PowerDistributionRoomVO>> list = powerDistributionRoomRpc.getPowerDistributionRoomList(roomName);
        for(PowerDistributionRoomVO vo : list.getData()){
            String roomAreaStr = dataDictRpc.getDisplayNameByEnumAndValue(pdroomDict, String.valueOf(vo.getRoomArea())).getData();
            vo.setRoomAreaStr(roomAreaStr);
        }
        return list;
    }

    /**
     *  新增配电室记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增配电室记录")
    public CommonResult<Integer>  createPowerDistributionRoom(@Valid PowerDistributionRoomCreateDTO roomDTO){
        return powerDistributionRoomRpc.createPowerDistributionRoom(roomDTO);
    }

    /**
     * 获取配电室所属片区字典信息
     * @return
     */
    @GetMapping("/getPdroomAreaDict")
    @ApiOperation("获取配电室所属片区字典信息")
    public CommonResult<List<DataDictVO>> getPowerDistributionRoomAreaDict(){
        return dataDictRpc.listDataDictsByEnumValue(pdroomDict);
    }

}
